/** @odoo-module **/

import { loadJS } from "@web/core/assets";

import { Component, onWillStart, useEffect } from "@odoo/owl";

export class Echarts extends Component {
    setup() {
        onWillStart(async () => {
            await loadJS("/echarts_action/static/lib/echarts/echarts.min.js");
            this.echartsOptions = await this.props.getEchartsOptions();
            this.echartsDatas = await this.props.getEchartsDatas();
            Object.assign(this.echartsOptions, { dataset: this.echartsDatas });
        });
        useEffect(
            () => {
                this.echarts = echarts.init(document.getElementById('echarts'));
                this.echarts.setOption(this.echartsOptions);
                return () => this.echarts.dispose();
            },
            () => []
        );
    }
}

Echarts.template = "echarts_action.Echarts";

Echarts.props = {
    getEchartsOptions: { type: Function },
    getEchartsDatas: { type: Function },
};